/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AjouConquerorBeans;

import Classes.ABuildingInR;
import Classes.BuildingsInR;
import data.BuildingEntity;
import data.BuildingEntityFacade;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.LocalBean;

/**
 *
 * @author ajou
 */
@Singleton
@LocalBean
public class BuildingSateInRBean {

    @EJB
    private BuildingEntityFacade bef;
    ArrayList<BuildingsInR> buildings = new ArrayList<BuildingsInR>();

    public boolean checkGameover(int roomid) {
        for (BuildingsInR b : buildings) {
            if (b.getRoomid() == roomid) {
                if (b.checkGameover()) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public BuildingsInR getBuildings(int roomid) {
        for (BuildingsInR b : buildings) {
            if (b.getRoomid() == roomid) {
                return b;
            }
        }
        return null;
    }

    public ArrayList<ABuildingInR> returnbuildings(int roomid) {
        ArrayList<ABuildingInR> tp = new ArrayList<ABuildingInR>();
        for (int i = 0; i < buildings.size(); i++) {
            if (buildings.get(i).getRoomid() == roomid) {
                tp = buildings.get(i).getBuilinds();
            }
        }

        return tp;
    }

    public void concquer(String buildingname, String uid, int point, int roomid) {
        ArrayList<ABuildingInR> ajoubuildings = new ArrayList<ABuildingInR>();
        for (int i = 0; i < buildings.size(); i++) {
            if (buildings.get(i).getRoomid() == roomid) {
                ajoubuildings = buildings.get(i).getBuilinds();
                for (int w = 0; w < ajoubuildings.size(); w++) {
                    if (ajoubuildings.get(w).getBuidlingname().equals(buildingname)) {
                        ajoubuildings.get(w).setConqueredUId(uid);
                        ajoubuildings.get(w).setPoint(point);
                    }
                }
            }
        }
    }

    public void addbuildng(int roomid) {
        Boolean flag = true;
        for (BuildingsInR p : buildings) {
            if (p.getRoomid() == roomid) {
                flag = false;
            }
        }

        if (flag) {
            ArrayList<ABuildingInR> buildingsEntity = new ArrayList<ABuildingInR>();
            
            for (BuildingEntity b : bef.findAll()) {
                buildingsEntity.add(new ABuildingInR(b.getName(), b.getQuestions()));
            }
            BuildingsInR BIR = new BuildingsInR();

            BIR.setBuilinds(buildingsEntity);
            BIR.setRoomid(roomid);

            buildings.add(BIR);

        }
    }
}
